package com.neuedu.xkxt.service.impl;

import com.neuedu.xkxt.dao.StudentCourseRelDao;
import com.neuedu.xkxt.dao.StudentDao;
import com.neuedu.xkxt.dao.StudentMapper;
import com.neuedu.xkxt.model.StudentCourseRel;
import com.neuedu.xkxt.model.StudentCourseRelExample;
import com.neuedu.xkxt.service.StudentService;
import com.neuedu.xkxt.vo.CourseVO;
import com.neuedu.xkxt.vo.PageVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
@Resource
    private StudentMapper studentMapper;
@Resource
private StudentCourseRelDao studentCourseRelDao;

    @Override
    public PageVO<CourseVO> pageSelectd(int pageNo, int pageSize, int studentId) {
        List<CourseVO> data=studentMapper.pageSelected(studentId,(pageNo-1)*pageSize,pageSize);
        int count=studentMapper.pageSelectedCount(studentId);
        PageVO<CourseVO> pageVO=new PageVO<>();
        pageVO.setData(data);
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        pageVO.setPageCount(count/pageSize+(count%pageSize==0?0:1));
        return pageVO;
    }

    @Override
    public PageVO<CourseVO> pageUnSelectd(int pageNo, int pageSize, int studentId) {
        // pageNo：页码，pageSize：每页记录数， startIndex：第n条记录的索引
        int startIndex=(pageNo-1)*pageSize;
        List<CourseVO> data=studentMapper.pageUnSelected(studentId,startIndex,pageSize);
        int count=studentMapper.pageUnSelectedCount(studentId);
        PageVO<CourseVO> pageVO=new PageVO<>();
        pageVO.setData(data);
        pageVO.setPageNo(pageNo);
        pageVO.setPageSize(pageSize);
        pageVO.setPageCount(count/pageSize+(count%pageSize==0?0:1));
        return pageVO;
    }

    @Override
    public int editCourseState(int studentId, int courseId, int state) {
        if (state==0){
            StudentCourseRelExample example=new StudentCourseRelExample();
            StudentCourseRelExample.Criteria criteria=example.createCriteria();
            criteria.andCidEqualTo(courseId);
            criteria.andSidEqualTo(studentId);
            return studentCourseRelDao.deleteByExample(example);
        }else{
            StudentCourseRelExample example=new StudentCourseRelExample();
            StudentCourseRelExample.Criteria criteria=example.createCriteria();
            criteria.andSidEqualTo(studentId);
            List<StudentCourseRel> studentCourseRels=studentCourseRelDao.selectByExample(example);
            if (studentCourseRels!=null&&studentCourseRels.size()>=5){
                return 0;
            }
            StudentCourseRel studentCourseRel=new StudentCourseRel();
            studentCourseRel.setCid(courseId);
            studentCourseRel.setSid(studentId);
            return studentCourseRelDao.insertSelective(studentCourseRel);

        }
    }

}
